package net.mihome.app;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.EMConnectionListener;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import net.mihome.app.api.ApiClient;
import net.mihome.app.bean.User;
import net.mihome.app.common.StringUtils;
import net.mihome.app.ui.LoginActivity;
import net.mihome.app.ui.MainPager;
import net.mihome.app.utils.LoginResponseHandler;

/**
 * 应用程序启动类：显示欢迎界面并跳转到主界面
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class AppStart extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        check();
        checkLogin();
    }
    /**
     * 判断用户是否登陆
     * */
    public void checkLogin(){
        if(AppContext.getInstance().checkLogin()){
            //已登录
//            if(AppContext.getInstance().getIsLogined()){
//                //如果是从休眠中唤醒,则直接跳转到mainpager
//                Toast.makeText(AppStart.this,"休眠恢复..",Toast.LENGTH_SHORT).show();
//                redictToMain();
//            }else{
                //如果刚启动状态，则加载好友信息
                AppContext.getInstance().loadContractList2(this,new LoginResponseHandler() {
                    @Override
                    public void onSuccess(User user) {
                        Toast.makeText(AppStart.this,"加载好友信息成功..",Toast.LENGTH_SHORT).show();
                        redictToMain();
                    }

                    @Override
                    public void onFailed(String msg) {
                        //加载好友失败则进入登陆界面
                        Toast.makeText(AppStart.this,"加载好友信息失败:"+msg,Toast.LENGTH_SHORT).show();
                        AppContext.getInstance().cleanCookie();
                        AppContext.getInstance().cleanLoginInfo();
                        AppContext.getInstance().clearAppCache();
                        redictToLogin();
                    }
                });
//            }
        }else{
            //未登录，则跳转到登陆界面
            redictToLogin();
        }
    }
    private void redictToLogin(){
        //未登录，则跳转到登陆界面
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
    private void redictToMain(){
        //未登录，则跳转到登陆界面
        Intent intent = new Intent(this, MainPager.class);
        startActivity(intent);
        finish();
    }
    /**
     * 加载背景图片
     */
    private void check() {
        ImageView communityBg= (ImageView) findViewById(R.id.app_start_view2);
        User user=AppContext.getInstance().getLoginInfo();
        if(user!=null&&user.getCommunityName()!=null){
            TextView textView= (TextView) findViewById(R.id.app_start_view_name);
            textView.setText(user.getCommunityName());
        }
        String imageUrl=AppContext.getInstance().getProperty(ApiClient.APPSTART_IMAGE_BACKGROUND_KEY);
        ImageLoader.getInstance().displayImage(imageUrl,communityBg,options);
    }
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.zuoan1)
            .showImageForEmptyUri(R.drawable.zuoan1) // resource or drawable
            .showImageOnFail(R.drawable.zuoan1) // resource or drawable
            .resetViewBeforeLoading(false)  // default
            .cacheInMemory(false) // default
            .cacheOnDisk(true) // default
            .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
            .bitmapConfig(Bitmap.Config.ARGB_8888) // default
            .displayer(new SimpleBitmapDisplayer()) // default
            .handler(new Handler()) // default
            .build();
}